package cn.itcast.controller;


import cn.itcast.domain.Room;
import cn.itcast.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/room")
public class RoomController {
    @Autowired
    private RoomService service;

    /**
     * 新增房间
     *
     * @param room
     */
    @RequestMapping( "/add")
    public String add(Room room) {
        //System.out.println(room);
        service.addRoom(room);
        return "redirect:/room/findAll";

    }
    @RequestMapping("/delete")
    public String deleRoom(int id){
        service.deleRoom(id);
        return "redirect:/room/findAll";
        //return "删除成功";
    }

    @RequestMapping("/findAll")
    @ResponseBody
    public ModelAndView findAll(ModelAndView modelAndView, HttpServletRequest request)  {
        List<Room> list = service.findAll(request);
        modelAndView.addObject("roomList",list);
        modelAndView.setViewName("room-list2");
        request.getSession().setAttribute("rooms",list);
        return modelAndView;
    }
    @RequestMapping("/update")
    public String update(Room room,ModelAndView modelAndView){
        service.updateRoom(room);
        return "redirect:/room/findAll";
    }
}
